<?php
declare (strict_types = 1);

namespace app\common\listener;
use Workerman\Events\Swoole;
use think\facade\Db;
class WsConnect
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    // 使用websocket  连接  客户端入口
    public function handle($event)
    {
        $this->chat =   app('app\common\model\chat\Chat');
        $this->chat_message =   app('app\common\model\chat\ChatMessage');
        $this->system_admin =  app('app\common\model\system\SystemAdmin');

        $this->system_admin->where('id',$event->get('id'))->save(['is_online'=>1]);
        $ws = app('think\swoole\Websocket');
       /* //未登录过的客服账号获取客服客户端id
        //已经拥有的客户端fd
        $fded_list = $this->chat->whereIn('type',[2,3])->column('user_id');
        //未拥有的客服
        //dd($fded_list);
        $usered_list = $this->system_admin->where(['status'=>1,'is_service'=>1])->whereNotIn('id',$fded_list)->column('id');
        echo $ws->getSender();

        //echo $event->get('id');*/
        echo '连接成功';
        //var_dump($event->get('id'));
        //$event : 请求对象  $event->get('id')
        // 数据id:$ws->getSender() 保管数据  mysql/table/redis:hash


        $data =['user_id'=>$event->get('id'),
            'type'=>$event->get('type')];
        $res = $this->chat->where($data)->find();
       
        $res && $data['id'] = $res->id;
        $data['fd'] = $ws->getSender();
        echo $data['fd'];
        if($res){
            $this->chat->where('id',$data['id'])->save($data);
        }else{
            $this->chat->save($data);
        }



       /* $table = new \Swoole\Table(1024);

        //数据操作
        $table->set('fd',[  //获取发送者的客户端信息
            'id'=>$event->get('id'),
            'name'=>'客户端',
            'num'=>$ws->getSender()
        ]);*/

        //var_dump($table->get('fd_1'));
        //var_dump($table->get('fd_2'));

        $ws->emit('sendfd', $ws->getSender());//发送
        //$ws->emit('sendfd', $table->get('fd'));//发送

    }    
}
